// pages/goods/detail.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenLoading: false,
    detail: [],
    goodsList: [],
    currentID: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //初始化
    this.setDetailData(options.id);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //用户授权登录
    app.login();
  },

  //下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    this.setDetailData(that.data.currentID);
    wx.stopPullDownRefresh();
  },

  //初始化详情
  setDetailData: function (id) {
    var that = this;
    var paraArr = new Array();
    paraArr['id'] = id;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_topic/'+id,
      data: { sign: sign },
      method: 'GET',
      header: {},
      success: function (res) {
        console.log(res.data);
        that.setData({
          detail: res.data,
          currentID: id
        });
        that.setGoodsData(res.data.goods_ids);
      }
    })
  },

  //初始化专题商品
  setGoodsData: function (ids) {
    var that = this;
    var paraArr = new Array();
    paraArr['ids'] = ids;
    var sign = app.signature(paraArr);
    wx.request({
      url: rootDocment + '/api_goods',
      data: { ids: ids, sign: sign },
      method: 'GET',
      header: {},
      success: function (res) {
        that.setData({
          goodsList: res.data.data,
          hiddenLoading: true
        });
      }
    })
  },
  //点击商品
  showGoodsDetial: function (e) {
    var id = e.currentTarget.dataset.id
    if (!id) return;
    app.redirect('goods/detail', 'id=' + id)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})